package com.weiguan.xmpp.util;

import java.io.IOException;

import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.sasl.provided.SASLPlainMechanism;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

public class XMPPTCPConnectionUtils {

	private static XMPPTCPConnection connection = null;

	// public static XMPPTCPConnection openConnection(String userName, String
	// userPass) {
	// String serviceName = "xmpp.weilive.cc";
	// XMPPTCPConnectionConfiguration.Builder conf =
	// XMPPTCPConnectionConfiguration.builder();
	// //conf.setServiceName(serviceName);
	// conf.setHost("120.131.9.35");
	// conf.setUsernameAndPassword(userName, userPass);
	// conf.setSecurityMode(SecurityMode.disabled);
	// conf.setCompressionEnabled(true);
	// conf.setSendPresence(false);
	//
	// SASLAuthentication.registerSASLMechanism(new SASLPlainMechanism());
	// XMPPTCPConnection connection = null;
	// try {
	// connection = new XMPPTCPConnection(conf.build());
	// return connection;
	// } catch (Exception e) {
	// throw new RuntimeException("创建Connection异常");
	// }
	// }

	private final static class LazyHolder {

		private static String serviceName = "weilive.tv";
		private static String userName = "10000001";
		private static String password = "10000001";

		// private static String serviceName = "weilive.tv";
		// private static String userName = "100";
		// private static String password = "123456";

		private static XMPPTCPConnection getSingleConnection() {

			synchronized (LazyHolder.class) {
				if (connection == null) {
					try {

						XMPPTCPConnectionConfiguration.Builder conf = XMPPTCPConnectionConfiguration.builder();
						conf.setServiceName(serviceName);
						conf.setHost("120.131.9.35");
						conf.setUsernameAndPassword(userName, password);
						conf.setSecurityMode(SecurityMode.disabled);
						conf.setCompressionEnabled(true);
						conf.setSendPresence(false);

						SASLAuthentication.registerSASLMechanism(new SASLPlainMechanism());

						connection = new XMPPTCPConnection(conf.build());
						connection.connect();
						connection.login();

						if (!connection.isAuthenticated()) {
							throw new RuntimeException("登录认证Openfire服务器异常");
						}
						System.out.println("登陆成功");
					} catch (Exception e) {
						throw new RuntimeException("登录openfire服务器异常", e);
					}
				}
			}
			return connection;
		}
	}

	private XMPPTCPConnectionUtils() {

	}

	public static XMPPTCPConnection getXMPPTCPConnection(String serviceName, String host, String userName,
			String password) {
		try {

			XMPPTCPConnectionConfiguration.Builder conf = XMPPTCPConnectionConfiguration.builder();
			conf.setServiceName(serviceName);
			conf.setHost(host);
			conf.setUsernameAndPassword(userName, password);
			conf.setSecurityMode(SecurityMode.disabled);
			conf.setCompressionEnabled(true);
			conf.setSendPresence(true);
			conf.setConnectTimeout(30 * 1000);
			//conf.setResource(UUID.randomUUID().toString().replace("-", ""));

			SASLAuthentication.registerSASLMechanism(new SASLPlainMechanism());

			XMPPTCPConnection connection = new XMPPTCPConnection(conf.build());
			connection.connect();
			connection.login();
			connection.setPacketReplyTimeout(30 * 1000L);

			if (!connection.isAuthenticated()) {
				throw new RuntimeException("登录认证Openfire服务器异常");
			}
			return connection;
		} catch (Exception e) {
			return null;
		}
	}

	public static XMPPTCPConnection getInstance() {
		return LazyHolder.getSingleConnection();
	}

	public static void main(String[] args) throws SmackException, IOException, XMPPException {
		// Long userId = 699L;
		// String userPass = "111111";
		// XMPPTCPConnectionConfiguration.Builder conf =
		// XMPPTCPConnectionConfiguration.builder();
		// conf.setServiceName("weilive.tv");
		// conf.setUsernameAndPassword(String.valueOf(userId), userPass);
		// conf.setSecurityMode(SecurityMode.disabled);
		// conf.setCompressionEnabled(true);
		// conf.setSendPresence(false);

		// SASLAuthentication.registerSASLMechanism(new SASLPlainMechanism());
		//
		// XMPPTCPConnection connection = new XMPPTCPConnection(conf.build());
		// connection.connect();
		// connection.login();
		// System.out.println(connection.isAuthenticated());

		getInstance();

	}

}
